#include "stdafx.h"
#include "ContentManager.h"

using namespace MomogenkyouSystem;

ContentManager::ContentManager()
{


}

ContentManager::~ContentManager()
{
	ClearContents();
}


void	ContentManager::ClearContents()
{
	for(std::map<std::string, TextureData *>::iterator it = m_Textures.begin();
		it!= m_Textures.end();
		it++)
	{
		it->second->Handle->Release();
		delete it->second;
		it->second = NULL;
	}
	m_Textures.clear();
}

void	ContentManager::LoadTextures(std::vector<std::string> Filenames)
{
	TEXTURE	_LoadedTexture	=	NULL;
	HRESULT	_Result			=	S_OK;

	for(size_t i = 0; i < Filenames.size(); i++)
	{
		_Result	=	D3DXCreateTextureFromFileA(
			GraphicsSystem::Device, 
			Filenames[i].c_str(), 
			&_LoadedTexture);

		ASSERT(_Result == S_OK, "Unable to load Texture ");
		m_Textures.insert(std::make_pair(Filenames[i].c_str(), new TextureData(_LoadedTexture)));
		_LoadedTexture = NULL;
	}
}

TextureData	*	ContentManager::LoadTexture(std::string Filename)
{
	TEXTURE	_LoadedTexture	=	NULL;
	HRESULT	_Result			=	S_OK;

	
	_Result	=	D3DXCreateTextureFromFileA(
		GraphicsSystem::Device, 
		Filename.c_str(), 
		&_LoadedTexture);

	ASSERT(_Result == S_OK, "Unable to load Texture ");
	
	m_Textures.insert(std::make_pair(Filename.c_str(), new TextureData(_LoadedTexture)));

	return	GetTexture(Filename);
}

TextureData	*	ContentManager::GetTexture(const std::string &FileName)
{
	std::map<std::string, TextureData *>::iterator it = m_Textures.find(FileName.c_str());
	if(it == m_Textures.end())
		return NULL;

	return it->second;
}